#!/bin/bash

# Redis-rs2 集群功能测试脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

echo -e "${CYAN}🧪 Redis-rs2 集群功能测试脚本${NC}"
echo -e "${CYAN}===================================${NC}"
echo

# 检查集群是否运行
echo -e "${BLUE}🔍 检查集群状态...${NC}"
running_nodes=0
for port in 7001 7002 7003 7004 7005 7006 7007; do
    if nc -z 127.0.0.1 $port 2>/dev/null; then
        running_nodes=$((running_nodes + 1))
    fi
done

if [ $running_nodes -lt 4 ]; then
    echo -e "${RED}❌ 集群未完全启动 (只有 $running_nodes/7 个节点运行)${NC}"
    echo -e "${YELLOW}请先运行: ./scripts/start_cluster.sh${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 集群状态正常 ($running_nodes/7 个节点运行)${NC}"
echo

# 编译测试客户端
echo -e "${BLUE}🔨 编译测试客户端...${NC}"
cd cluster_test_client

if ! cargo build --release; then
    echo -e "${RED}❌ 测试客户端编译失败${NC}"
    exit 1
fi

echo -e "${GREEN}✅ 测试客户端编译成功${NC}"
echo

# 测试菜单
while true; do
    echo -e "${CYAN}🧪 请选择测试类型：${NC}"
    echo "1. 基础功能测试"
    echo "2. 压力测试 (轻量级)"
    echo "3. 压力测试 (中等强度)"
    echo "4. 压力测试 (高强度)"
    echo "5. 数据分片测试"
    echo "6. 集群信息查询"
    echo "7. 故障转移测试"
    echo "8. 运行所有测试"
    echo "9. 退出"
    echo
    read -p "请输入选择 (1-9): " choice
    
    case $choice in
        1)
            echo -e "${YELLOW}🧪 运行基础功能测试...${NC}"
            ./target/release/redis-cluster-client-test basic
            ;;
        2)
            echo -e "${YELLOW}💪 运行轻量级压力测试 (10并发, 30秒, 50QPS)...${NC}"
            ./target/release/redis-cluster-client-test stress -c 10 -d 30 -q 50
            ;;
        3)
            echo -e "${YELLOW}💪 运行中等强度压力测试 (20并发, 60秒, 100QPS)...${NC}"
            ./target/release/redis-cluster-client-test stress -c 20 -d 60 -q 100
            ;;
        4)
            echo -e "${YELLOW}💪 运行高强度压力测试 (50并发, 120秒, 200QPS)...${NC}"
            ./target/release/redis-cluster-client-test stress -c 50 -d 120 -q 200
            ;;
        5)
            echo -e "${YELLOW}🔀 运行数据分片测试 (10000个键)...${NC}"
            ./target/release/redis-cluster-client-test sharding -k 10000
            ;;
        6)
            echo -e "${YELLOW}ℹ️  查询集群信息...${NC}"
            ./target/release/redis-cluster-client-test info
            ;;
        7)
            echo -e "${YELLOW}⚠️  运行故障转移测试...${NC}"
            echo -e "${CYAN}注意：此测试需要手动操作，请按提示进行${NC}"
            ./target/release/redis-cluster-client-test failover
            ;;
        8)
            echo -e "${YELLOW}🚀 运行所有测试...${NC}"
            echo
            
            # 基础功能测试
            echo -e "${PURPLE}=== 基础功能测试 ===${NC}"
            ./target/release/redis-cluster-client-test basic
            echo
            
            # 轻量级压力测试
            echo -e "${PURPLE}=== 轻量级压力测试 ===${NC}"
            ./target/release/redis-cluster-client-test stress -c 10 -d 30 -q 50
            echo
            
            # 数据分片测试
            echo -e "${PURPLE}=== 数据分片测试 ===${NC}"
            ./target/release/redis-cluster-client-test sharding -k 5000
            echo
            
            # 集群信息查询
            echo -e "${PURPLE}=== 集群信息查询 ===${NC}"
            ./target/release/redis-cluster-client-test info
            echo
            
            echo -e "${GREEN}🎉 所有自动化测试完成！${NC}"
            echo -e "${CYAN}💡 要测试故障转移，请单独运行选项7${NC}"
            ;;
        9)
            echo -e "${GREEN}👋 退出测试程序${NC}"
            break
            ;;
        *)
            echo -e "${RED}❌ 无效选择，请输入 1-9${NC}"
            ;;
    esac
    
    echo
    echo -e "${CYAN}按回车键继续...${NC}"
    read
    clear
done

cd ..

echo -e "${GREEN}✨ 测试完成！${NC}"
echo
echo -e "${CYAN}📝 有用的命令：${NC}"
echo -e "   📊 查看集群状态: ${BLUE}./scripts/cluster_status.sh${NC}"
echo -e "   📋 查看节点日志: ${BLUE}tail -f logs/cluster/node1.log${NC}"
echo -e "   🛑 停止集群: ${BLUE}./scripts/stop_cluster.sh${NC}"